﻿
#region 作者和版权
/*************************************************************************************
* CLR 版本: 4.0.30319.42000
* 类 名 称: SdkConfig
* 机器名称: SUNTIA-QL
* 命名空间: ServerFramework.Tools.Sdk
* 文 件 名: SdkConfig
* 创建时间: 2016/1/18 15:46:08
* 作 者: 乔龙 Qiaolong
* 版 权: SdkConfig说明：本代码版权归乔龙所有，使用时必须带上乔龙网站地址 All Rights Reserved (C) 2016 - 2013
* 签 名: To be or not, it is not a problem !
* 网 站: http://www.suntia.net
* 邮 箱: qiaolong306@qq.com 
* 唯一标识： f559d088-49f5-47e2-a88d-e726019426db 
*
* 登录用户: simida
* 所 属 域: SUNTIA-QL

* 创建年份: 2016
* 修改时间:
* 修 改 人:
* 
************************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Configuration;
/*
  <configSections>
    <section name="sdkChannel" type="ServerFramework.Tools.Sdk.SdkSection,ServerFramework"/>
  </configSections>

  <sdkChannel>
      <add sid ="1" sname ="2" appid = "" appkey = "" appSecret = "" url = ""/>
      <add sid ="2" sname ="2" appid = "" appkey = "" appSecret = "" url = ""/>
  </sdkChannel>
 */

namespace ServerFramework.Tools.Sdk
{
    public class SdkSection : ConfigurationSection	// 所有配置节点都要选择这个基类
    {
        private static SdkSection _instance;

        public static SdkSection Instance { get 
        {
            if (_instance == null)
            {
                _instance = (SdkSection)System.Configuration.ConfigurationManager.GetSection("sdkChannel");
            }
            return _instance;
        } }

        private static readonly ConfigurationProperty s_property
            = new ConfigurationProperty(string.Empty, typeof(ChannelCollection), null,
                                            ConfigurationPropertyOptions.IsDefaultCollection);

        [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        public ChannelCollection Collection
        {
            get
            {
                return (ChannelCollection)base[s_property];
            }
        }
    }


    [ConfigurationCollection(typeof(ChannelInfo))]
    public class ChannelCollection : ConfigurationElementCollection		// 自定义一个集合
    {
        // 基本上，所有的方法都只要简单地调用基类的实现就可以了。

        public ChannelCollection()
            : base(StringComparer.OrdinalIgnoreCase)	// 忽略大小写
        {
        }

        // 其实关键就是这个索引器。但它也是调用基类的实现，只是做下类型转就行了。
        new public ChannelInfo this[string name]
        {
            get
            {
                return (ChannelInfo)base.BaseGet(name);
            }
        }

        // 下面二个方法中抽象类中必须要实现的。
        protected override ConfigurationElement CreateNewElement()
        {
            return new ChannelInfo();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ChannelInfo)element).Sid;
        }

        // 说明：如果不需要在代码中修改集合，可以不实现Add, Clear, Remove
        public void Add(ChannelInfo setting)
        {
            this.BaseAdd(setting);
        }
        public void Clear()
        {
            base.BaseClear();
        }
        public void Remove(string name)
        {
            base.BaseRemove(name);
        }
    }

    public class ChannelInfo : ConfigurationElement	// 集合中的每个元素
    {
        [ConfigurationProperty("sid", IsRequired = true)]
        public string Sid
        {
            get { return this["sid"].ToString(); }
            set { this["sid"] = value; }
        }

        [ConfigurationProperty("sname", IsRequired = true)]
        public string Sname
        {
            get { return this["sname"].ToString(); }
            set { this["sname"] = value; }
        }

        [ConfigurationProperty("appid", IsRequired = true)]
        public string AppId
        {
            get { return this["appid"].ToString(); }
            set { this["appid"] = value; }
        }


        [ConfigurationProperty("appkey", IsRequired = true)]
        public string AppKey
        {
            get { return this["appkey"].ToString(); }
            set { this["appkey"] = value; }
        }

        [ConfigurationProperty("appSecret", IsRequired = true)]
        public string AppSecret
        {
            get { return this["appSecret"].ToString(); }
            set { this["appSecret"] = value; }
        }

        [ConfigurationProperty("url", IsRequired = true)]
        public string Url
        {
            get { return this["url"].ToString(); }
            set { this["url"] = value; }
        }
    

    }


}


